<template>
  <div style="min-height: 350px">
    <el-form ref="userForm" size="default" :model="entity" :rules="rules" label-width="120px" @submit.native.prevent>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="entity.username" placeholder="用户名" ref="username" />
      </el-form-item>
      <el-form-item label="密码" prop="pwd">
        <el-input v-model="entity.pwd" show-password placeholder="密码" />
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="entity.name" placeholder="姓名" />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="entity.phone" placeholder="手机号" />
      </el-form-item>
      <el-form-item label="企业微信" prop="wxuid">
        <el-input v-model="entity.wxuid" placeholder="企业微信" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="btnSubmit" size="default">确 定</el-button>
        <el-button @click="onCancel" size="default">取 消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup name="userDetail">
import request from '/@/utils/request';
import { ElMessage } from 'element-plus';
import mittBus from '/@/utils/mitt';
import {reactive, ref, onMounted, defineProps, defineEmits, nextTick} from "vue";

// 定义变量内容
const userForm = ref(null);
const username = ref(null);

// defineProps(['modelValue'])
const emit = defineEmits(['update:modelValue'])
let props = defineProps({
  modelValue : {type : Boolean, default: true},
  entityId : {type : Number, default: 0},
  orgId : {type : Number, default: 0}
})

let entity = ref({
  username: '',
  pwd : '',
  name: '',
  phone:'',
  wxuid:'',
  orgId : props.orgId
})
const rules = reactive({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
  ],
  pwd: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
  ],
  name: [
    { required: true, message: '请输入姓名', trigger: 'blur' },
    { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
  ],
})

const loadEntity = ()=>{
  if(props.entityId > 0){
    request({ url: '/sys/useradmin/getuser', data: {id : props.entityId} }).then( res =>{
      entity.value = res.data
    })
  }
}
const onCancel = ()=>{
  emit('update:modelValue', false) //关闭弹窗
}
const btnSubmit = ()=>{
  if(!userForm.value) return;
  userForm.value.validate((valid)=>{
    if(valid){
      request({
        url: '/sys/useradmin/addedituser',
        data: entity.value
      }).then( res =>{
        if(res.code === "200"){
          ElMessage.success(res.msg)
          emit('update:modelValue', false) //关闭弹窗
          //触发事件总线，让列表界面更新数据
          props.entityId > 0 ? mittBus.emit('useredit', {}) : mittBus.emit('useradd', {});
        }else{
          ElMessage.error(res.msg);
        }
      })
    }else{
      console.log('error submit!!');
      return false;
    }
  })
}

onMounted(() => {
  loadEntity();
  nextTick(()=>{
    username.value.focus()
  })
});

</script>

<style scoped>
</style>
